﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityMultiPlayerGameServer.Clients;
using GameProtocol;
using UnityMultiPlayerGameServer.Data;

namespace UnityMultiPlayerGameServer.Manager
{
    /// <summary>
    /// 控制账户相关的功能
    /// </summary>
    internal class UserController : BaseController
    {
        public UserController()
        {
            messageType = MessageType.User;
        }
        /// <summary>
        /// 注册账号，将注册成功的账号id一并返回
        /// </summary>
        /// <param name="client"></param>
        /// <param name="pack"></param>
        /// <returns>GamePack</returns>
        public GamePack ActionLogon(Client client, GamePack pack)
        {
            pack.ReturnCode = ReturnCode.ReturnFail;

            if (!DataBaseController.Instance.TryFindUser(pack.UserInfo.UserName, out bool flag))
            {
                //判断查找函数有无异常产生
                if (flag == false)
                {
                    UserInfo rturnData = DataBaseController.Instance.InsertData(pack.UserInfo.UserName, pack.UserInfo.Password);
                    if (rturnData != null)
                    {
                        pack.UserInfo.AccountID = rturnData.AccountID;
                        pack.UserInfo.Password = "";

                        pack.ReturnCode = ReturnCode.ReturnSucceed;
                    }
                }
            }

            Console.WriteLine("ActionLogon: " + client.Socket.RemoteEndPoint);
            return pack;
        }

        /// <summary>
        /// 登录账号，将账号id一并返回，并为client的userData赋值
        /// </summary>
        /// <param name="client"></param>
        /// <param name="pack"></param>
        /// <returns>GamePack</returns>
        public GamePack ActionLogin(Client client, GamePack pack)
        {
            UserInfo returnData = DataBaseController.Instance.GetUserDataFBase(pack.UserInfo.UserName);
            if(returnData != null)
            {
                if(returnData.Password == pack.UserInfo.Password)
                {
                    Console.WriteLine(pack.UserInfo.UserName + " :has login succeed");

                    client.userInfo.AccountID = returnData.AccountID;
                    Console.WriteLine("accountID:" + returnData.AccountID);
                    client.userInfo.UserName = returnData.UserName;
                    client.IsLogin = true;

                    pack.ReturnCode = ReturnCode.ReturnSucceed;
                    pack.UserInfo.AccountID = returnData.AccountID;

                    ClientManager.Instance.AddClientDict(client);
                }
                else
                {
                    Console.WriteLine(pack.UserInfo.UserName + " :login fail，wrong password");
                    client.IsLogin = false;
                    pack.ReturnCode = ReturnCode.ReturnFail;
                }              
            }
            else
            {
                client.IsLogin = false;
                pack.ReturnCode = ReturnCode.ReturnFail;
            }
            Console.WriteLine("ActionLogin: " + client.Socket.RemoteEndPoint);
            return pack;
        }
    }
}
